ALTER PROCEDURE [dbo].[GetSessionBettingForClient]    
 -- Add the parameters for the stored procedure here    
 @AdminId INT,
 @MatchId INT,
 @BettingTypeId INT
     
AS    
BEGIN    
 -- SET NOCOUNT ON added to prevent extra result sets from    
 -- interfering with SELECT statements.    
 SET NOCOUNT ON;    
    
    -- Insert statements for procedure here    
    IF EXISTS (
		SELECT Id from Betting where IsDeleted=0
    )
    BEGIN
        --PRINT 'New'
		select b.Id,b.CreatedBy,
		CASE b.LagadiRate 
			WHEN CAST(0 as VARCHAR(5)) THEN b.DrawLagadi ELSE b.LagadiRate
		END as LagadiRate,
		CASE b.KhadhiRate
			WHEN CAST(0 as VARCHAR(5)) THEN b.DrawKhadhi ELSE b.KhadhiRate
		END as KhadhiRate,
		b.TeamId,b.MatchScheduleId,
		(t.TeamName + ' VS ' + t1.TeamName) as Team,t.TeamName as Team1,t1.TeamName as Team2,
		b.[Over] as [Over],
		TT.TeamName
		from Betting b
		inner join MatchSchedule m on b.MatchScheduleId = m.Id
		inner join Team t on m.Team1 = t.Id
		inner join Team t1 on m.Team2 = t1.Id
		inner join Team TT on b.TeamId = TT.Id
		where b.IsDeleted = 0 and b.CreatedBy= @AdminId and b.BettingTypeId = @BettingTypeId and 
		b.MatchScheduleId = @MatchId
     END
     ELSE
     BEGIN
        --PRINT 'OLD'
		select top 1 b.Id,b.CreatedBy,
		CASE b.LagadiRate 
			WHEN CAST(0 as VARCHAR(5)) THEN b.DrawLagadi ELSE b.LagadiRate
		END as LagadiRate,
		CASE b.KhadhiRate
			WHEN CAST(0 as VARCHAR(5)) THEN b.DrawKhadhi ELSE b.KhadhiRate
		END as KhadhiRate,
		b.TeamId,b.MatchScheduleId,
		(t.TeamName + ' VS ' + t1.TeamName) as Team,t.TeamName as Team1,t1.TeamName as Team2,
		b.[Over] as [Over],
		TT.TeamName
		from Betting b
		inner join MatchSchedule m on b.MatchScheduleId = m.Id
		inner join Team t on m.Team1 = t.Id
		inner join Team t1 on m.Team2 = t1.Id
		inner join Team TT on b.TeamId = TT.Id
		where b.CreatedBy= @AdminId and b.BettingTypeId = @BettingTypeId and 
		b.MatchScheduleId = @MatchId and Cast(b.CreatedDate as date) = cast(GETDATE() as date)
		ORDER BY Id desc
     END
END
